package com.whl.sm.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.controller.BaseController;
import com.whl.sm.model.system.OrgUser;
import com.whl.sm.model.system.OrgUserInfo;
import com.whl.sm.model.system.Role;
import com.whl.sm.model.system.UserCpRole;
import com.whl.sm.service.system.OrgUserService;
import com.whl.sm.service.system.RoleService;
import com.whl.sm.service.system.UserCpRoleService;
import com.whl.sm.vo.OrgUserRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-09-10
 */
@RestController
@RequestMapping("/org-user")
public class OrgUserController extends BaseController {

    @Autowired
    OrgUserService orgUserService;
    @Autowired
    UserCpRoleService userCpRoleService;
    @Autowired
    RoleService roleService;

    @RequestMapping("/list")
    public Result getUserPages(HttpServletRequest request){
        QueryWrapper queryWrapper = getLikeParam("login_name");
        queryWrapper.ne("is_del",1);
        queryWrapper.orderByDesc("reg_date");
        Page<Object> userPage = orgUserService.page(getPage(),queryWrapper);
        List<Object> collect = userPage.getRecords().stream().map(obj ->{
            OrgUser orgUser = (OrgUser) obj;
            List<UserCpRole> userCpRoleList =  userCpRoleService.list(new QueryWrapper<UserCpRole>().eq("user_id",orgUser.getId()));
            Role role = null;
            if (userCpRoleList.size()>0){
                role = roleService.getById(userCpRoleList.get(0).getRoleId());
            }
            return new OrgUserRoleVo(orgUser,role);
        }).collect(Collectors.toList());
        userPage.setRecords(collect);
        return Result.ok(userPage);
    }


    @RequestMapping("/info/{id}")
    public Result userInfo(@PathVariable("id") String id){
        OrgUser user = orgUserService.getById(id);
        return Result.ok(user);
    }

    @RequestMapping("/update")
    public Result updateUser(@RequestBody OrgUser orgUser){
        orgUserService.update(orgUser);
        return Result.ok();
    }

    @RequestMapping("/wxRegister")
    public Result wxRegister(OrgUser orgUser){
        return orgUserService.register(orgUser);
    }

    @RequestMapping("/logDelete")
    public Result logicalDelete(@RequestBody List<String> idList){
        for (int i = 0; i <idList.size() ; i++) {
            orgUserService.update(new UpdateWrapper<OrgUser>()
                    .eq("id",idList.get(i))
                    .set("is_del",1)
            );
        }
        return Result.ok();
    }

    @RequestMapping("/restPwd/{id}")
    public Result restPwd(@PathVariable("id") String id){
        orgUserService.update(new UpdateWrapper<OrgUser>()
                .eq("id",id)
                .set("password",888888)
        );
        return Result.ok();
    }




}
